package com.me.jingdongshop.ui.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.widget.ImageView;

import com.me.jingdongshop.R;

import java.lang.reflect.Field;

/**
 * Created by YL on 2017/5/5.
 */

public class FloatImage extends ImageView {
    private int mImgWidth;
    private int mImgHeight;
    private Bitmap mImageBitmap;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mLayoutParams;

    private int mOffsetToParent;
    private int mOffsetToParentY;
    //状态栏高度
    private int mStatusBarHeight;
    private long mStartTime;

    public FloatImage(Context context) {
        this(context, null);
    }

    public FloatImage(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mWindowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        setImageResource(R.drawable.emoji_see_no_evil);
        setClickable(true);
        mStatusBarHeight = getStatusBarHeight();
        mOffsetToParent = dp2px(50);
        mOffsetToParentY = mStatusBarHeight + mOffsetToParent;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartTime = System.currentTimeMillis();
                break;
            case MotionEvent.ACTION_MOVE:
                mLayoutParams.x = (int) (event.getRawX() - mOffsetToParent);
                mLayoutParams.y = (int) (event.getRawY() - mOffsetToParentY);
                mWindowManager.updateViewLayout(FloatImage.this, mLayoutParams);
                break;
            case MotionEvent.ACTION_UP:
                long endTime = System.currentTimeMillis();
                if (endTime - mStartTime < 300) {
                    if (mFloatImageListener != null) {
                        mFloatImageListener.onClick();
                    }
                }
                break;
        }
        return true;
    }

    public void setLayoutParams(WindowManager.LayoutParams params) {
        mLayoutParams = params;
    }

    /**
     * 获取通知栏高度
     *
     * @return
     */
    private int getStatusBarHeight() {
        int statusBarHeight = 0;
        try {
            Class<?> c = Class.forName("com.android.internal.R$dimen");
            Object o = c.newInstance();
            Field field = c.getField("status_bar_height");
            int x = (Integer) field.get(o);
            statusBarHeight = getResources().getDimensionPixelSize(x);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return statusBarHeight;
    }

    private int dp2px(int dp) {
        return (int) (getContext().getResources().getDisplayMetrics().density * dp + 0.5f);
    }

    //回调点击事件
    private FloatImageListener mFloatImageListener;

    public void setFloatImageListener(FloatImageListener floatImageListener) {
        mFloatImageListener = floatImageListener;
    }

    public interface FloatImageListener {
        void onClick();
    }
}
